package com.example.hosptial.newController;

import com.example.hosptial.model.view.DoctorView;
import com.example.hosptial.service.AuthService;
import jakarta.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * 医生登录控制器
 */
@Controller
@RequestMapping("/doctor/auth")
public class DoctorAuthController {

    private static final Logger logger = LoggerFactory.getLogger(DoctorAuthController.class);

    @Autowired
    private AuthService authService;

    /**
     * 显示登录页面
     */
    @GetMapping("/login")
    public String showLoginPage() {
        return "common/login";
    }

    /**
     * 医生登录
     */
    @PostMapping("/login")
    public String login(@RequestParam String doctorId,
                       @RequestParam String password,
                       HttpSession session,
                       Model model,
                       RedirectAttributes redirectAttributes) {
        try {
            return authService.doctorLogin(doctorId, password)
                .map(doctor -> {
                    // 登录成功，保存session
                    session.setAttribute("doctor", doctor);
                    // 将医生信息添加到model中
                    model.addAttribute("doctor", doctor);
                    return "doctor/dashboard";
                })
                .orElseGet(() -> {
                    redirectAttributes.addFlashAttribute("error", "工号或密码错误");
                    return "redirect:/doctor/auth/login";
                });

        } catch (IllegalArgumentException e) {
            logger.warn("登录参数验证失败: {}", e.getMessage());
            redirectAttributes.addFlashAttribute("error", e.getMessage());
            return "redirect:/doctor/auth/login";
        } catch (Exception e) {
            logger.error("登录失败: {}", e.getMessage());
            redirectAttributes.addFlashAttribute("error", "系统错误，请稍后重试");
            return "redirect:/doctor/auth/login";
        }
    }

    /**
     * 医生登出
     */
    @PostMapping("/logout")
    public String logout(HttpSession session, RedirectAttributes redirectAttributes) {
        try {
            DoctorView doctor = (DoctorView) session.getAttribute("doctor");
            if (doctor != null) {
                authService.doctorLogout(doctor.getDoctorId());
                session.removeAttribute("doctor");
            }
            redirectAttributes.addFlashAttribute("success", "登出成功");
            return "redirect:/doctor/auth/login";
        } catch (Exception e) {
            logger.error("登出失败: {}", e.getMessage());
            redirectAttributes.addFlashAttribute("error", "登出失败");
            return "redirect:/doctor/auth/login";
        }
    }

    /**
     * 获取当前登录医生信息
     */
    @GetMapping("/current")
    public String getCurrentDoctor(HttpSession session, Model model) {
        DoctorView doctor = (DoctorView) session.getAttribute("doctor");
        if (doctor != null) {
            model.addAttribute("doctor", doctor);
            return "doctor/doctor-info";
        }
        return "redirect:/doctor/auth/login";
    }
}